package com.example.weather;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.weather.bean.WeaBean;
import com.example.weather.db.DBManager;
import com.example.weather.db.DateBaseBean;
import com.google.gson.Gson;

import java.util.List;

public class CityManagerAdapter extends BaseAdapter {
    private static final String TAG ="CityManagerAdapter" ;
    TextView cityTv,currentTempTv;
    Context context;
    List<DateBaseBean> mDatas;
    WeaBean weaBean;

    public CityManagerAdapter(Context context, List<DateBaseBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }
    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i(TAG, "getView: 适配器启动");
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_manager,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        DateBaseBean bean = mDatas.get(position);
        holder.cityTv.setText(bean.getCity());
        //使用gson解析数据
        weaBean = new Gson().fromJson(bean.getContent(), WeaBean.class);
        WeaBean.NewslistDTO newslistDTO=weaBean.getNewslist().get(0);
        //获取信息
//        holder.cityTv.setText(newslistDTO.getArea());
        Log.i(TAG, "getView: 地区"+newslistDTO.getArea());
        holder.currentTempTv.setText(newslistDTO.getReal());
        Log.i(TAG, "getView: 实时温度"+newslistDTO.getReal());
        return convertView;
    }
    public void remove(int index){  //删除指定项
        if(index < 0){
            return;
        }
        else {
            mDatas.remove(index);
            notifyDataSetChanged();  //更新
        }
    }

    class ViewHolder{
        TextView cityTv,currentTempTv;
        public ViewHolder(View itemView){
            cityTv = itemView.findViewById(R.id.item_city_tv_city);
            currentTempTv = itemView.findViewById(R.id.item_city_tv_temp);
        }
    }
}
